package whut.pso;

import com.google.common.primitives.Doubles;
import org.apache.commons.math3.util.FastMath;

import java.util.Arrays;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * Created by Justin on 2018-04-03
 */
public class GamesNormalization implements INormalization {
    private int LocSize;

    public GamesNormalization(int locSize) {
        LocSize = locSize;
    }

    @Override
    public double[] normalizeLocationOfRange(double[] loc, double[] center, double range) {
        if (center != null){
            for (int i = 0; i < loc.length; i++) {
                double up = FastMath.min(center[i] + range, 1);
                double low = FastMath.max(center[i] - range, 0);
                if (loc[i] < low){
                    loc[i] = low;
                }
                if (loc[i] > up){
                    loc[i] = up;
                }
            }
        }

//        int DimA = 1 + (loc.length >> 1);
        int DimA = LocSize + 1;
        int DimB = DimA;

        double[][] xy = PSOUtility.getXY(loc);
        double[] x = xy[0];
        double[] y = xy[1];

        for (int i = 0; i < x.length; i++) {
            x[i] = x[i] < 0 ? 0 : x[i];
        }
        for (int i = 0; i < y.length; i++) {
            y[i] = y[i] < 0 ? 0 : y[i];
        }
        double[] newx = PSOUtility.normalize(x);
        double[] newy = PSOUtility.normalize(y);
        x = Arrays.copyOfRange(newx, 0, DimA - 1);
        y = Arrays.copyOfRange(newy, 0, DimB - 1);
        return Doubles.concat(x, y);
    }
}
